package com.nsoft.thickclient.trackMove.search;

import javax.swing.JPanel;

import com.nsoft.thickclient.controller.ScreenController;
import com.nsoft.thickclient.table.TableRowSelectionEvent;
import com.nsoft.thickclient.table.TableRowSelectionListener;

public class SearchViewController<T> extends ScreenController implements
		TableRowSelectionListener {
	private SearchAction<T> action;

	private SearchView<T> view;

	private String title;

	public SearchViewController(SearchAction<T> action, String title) {
		this.action = action;
		this.title = title;
	}

	@Override
	public void close() {
		action = null;
		view = null;
	}

	@Override
	public String getScreenTitle() {
		return title;
	}

	@Override
	public JPanel getView() {
		return view;
	}

	@Override
	public void init() {
		view = action.getView(this);

	}

	public void doSearch() {
		view.setTableData(action.search(view.getWorkingBean()));
	}

	public void doEdit() {
		if(view.getSelectedBean()!=null) {
			action.edit(view.getSelectedBean(),getApplicationController());
		}
	}

	public void doDelete() {
		action.delete(view.getSelectedBean());
	}

	public void doClear() {
		view.setWorkingBean(action.getNewBean());
	}

	public void doCancel() {
		getApplicationController().closeScreen(this);
	}

	public void rowDeSelected(TableRowSelectionEvent evnt) {
		// TODO Auto-generated method stub

	}

	public void rowSelected(TableRowSelectionEvent evnt) {
		if (evnt.getClickCount() == 2) {
			doEdit();
		}

	}

}
